//
//  ViewController.m
//  YMCommentDemo
//
//  Created by lantaiyuan on 2017/2/24.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

#import "ViewController.h"
#import "YMCommentViewController.h"
#import "YMCommentView.h"

@interface ViewController ()<YMCommentViewControllerDelegate>
@property (nonatomic, strong) UIView * childVcView;
@property (nonatomic, strong) YMCommentViewController * commnetVc;
@end

@implementation ViewController
#pragma mark - Lazy

- (UIView *)childVcView{
    if (!_childVcView) {
        _childVcView = [[UIView alloc] initWithFrame:self.view.bounds];
    }
    return _childVcView;
}

- (YMCommentViewController *)commnetVc{
    if (!_commnetVc) {
        _commnetVc = [[YMCommentViewController alloc] init];
        _commnetVc.delegate = self;
        _commnetVc.view.frame = self.childVcView.frame;
        _commnetVc.view.backgroundColor = [UIColor colorWithWhite:0.7 alpha:0.7];
        self.childVcView = _commnetVc.view;
    }
    return _commnetVc;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton * commentBtn = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 80, 30)];
    [commentBtn setTitle:@"评价" forState:UIControlStateNormal];
    [commentBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [commentBtn addTarget:self action:@selector(commentAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:commentBtn];
    
    __weak typeof(self) weakSelf = self;
    self.commnetVc.touchTapBlock = ^{
        [weakSelf.commnetVc removeFromParentViewController];
        [weakSelf.childVcView removeFromSuperview];
        weakSelf.commnetVc = nil;
        weakSelf.childVcView = nil;
    };

}

- (void)commentAction:(UIButton *)button{
    [self addChildViewController:self.commnetVc];
    [self.view addSubview:self.childVcView];
//    self.view.backgroundColor = [UIColor colorWithWhite:0.7 alpha:0.7];
//    YMCommentView * commentView = [[YMCommentView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 350, 375, 350)];
//    [self.view addSubview:commentView];
}

#pragma mark - YMCommentViewControllerDelegate

- (void)cancel{
    [self.commnetVc removeFromParentViewController];
    [self.childVcView removeFromSuperview];
    self.commnetVc = nil;
    self.childVcView = nil;
}

@end
